/*
 * timer.h
 */

#ifndef TIMER_H_
#define TIMER_H_


#include <time.h>
#include <sys/time.h>
#include <list>
#include <iostream>

using namespace std;

class Timer {
public:

	struct timeval detail_time;
	time_t ltime;
	struct tm *Tm;
	int hourIni;
	int minuteIni;
	int secondIni;
	int usIni;

	class fiftyHzListener{
	public:
		virtual void fiftyHzUpdate(Timer &timer)=0;
		virtual ~fiftyHzListener() {}
	};

	class oneHzListener{
	public:
		virtual void oneHzUpdate(Timer &timer)=0;
		virtual ~oneHzListener() {}
	};

	class fiveHzListener{
	public:
		virtual void fiveHzUpdate(Timer &timer)=0;
		virtual ~fiveHzListener() {}
	};

	Timer();
	virtual ~Timer();

	double getTime();

	void startTiming();
	void stopTiming();

	void addListenerOneHz(oneHzListener *listener);
	void removeListenerOneHz(oneHzListener *listener);
	void notifyListenersOneHz();

	void addListenerFiveHz(fiveHzListener *listener);
	void removeListenerFiveHz(fiveHzListener *listener);
	void notifyListenersFiveHz();

	void addListenerFiftyHz(fiftyHzListener *listener);
	void removeListenerFiftyHz(fiftyHzListener *listener);
	void notifyListenersFiftyHz();

private:
	list<fiftyHzListener*> listenersFiftyHz;
	list<fiveHzListener*> listenersFiveHz;
	list<oneHzListener*> listenersOneHz;
	static void *startFiftyHzThread(void *);
	static void *startFiveHzThread(void *);
	static void *startOneHzThread(void *);
	void startFiftyHzLoop();
	void startFiveHzLoop();
	void startOneHzLoop();
};

#endif /* TIMER_H_ */
